package hbt;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class HibernateDAO {
	protected static HibernateDAO instancia;
	protected SessionFactory sf;
	protected Session session;


	protected HibernateDAO(){
		sf = HibernateUtil.getSessionFactory();
	}

	public static HibernateDAO getInstancia() {
		if (instancia == null){
			instancia = new HibernateDAO();
		}
		return instancia;
	}
	
	public SessionFactory getSf() {
		return sf;
	}
	public void setSf(SessionFactory sf) {
		this.sf = sf;
	}
	public Session getSession() {
		if (session == null || !session.isOpen()){
			session = sf.openSession();
		}
		return session;
	}
	public void closeSession(){
		if (session.isOpen()){
			session.close();
		}
	}

	public void persist(Object obj) {
		Session session = getSession();
		session.beginTransaction();
		session.persist(obj);
		session.getTransaction().commit();
	}

	@SuppressWarnings("rawtypes")
	public void persistList(List lista){
		Session session = getSession();
		session.beginTransaction();
		for(Object e : lista){
			session.merge(e);
		}
		session.flush();
		session.getTransaction().commit();
	}

	@SuppressWarnings("rawtypes")
	public Object get (Class c, Object o) {
		Session session = getSession();
		return session.get(c, (Serializable) o);
	}

	public List<?> getLista(String query) {
		Session session = getSession();
		List<?> lista = session.createQuery(query).list();
		return lista;
	}
	
	public List<?> parametros(String query, String prmName, String prm) {
		Session session = getSession();
		List<?> lista = session.createQuery(query).setParameter(prmName, prm).list();
		return lista;
	}
	
	public Object propiedades(String query, Object obj) {
		Session session = getSession();
		Object object = session.createQuery(query).setProperties(obj).uniqueResult();
		return object;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> tuplas(String query) {
		Session session = getSession();
		List<Object[]> lista = session.createQuery(query).list();
		return lista;
	}
	
	public Object totalizador(String query) {
		Session session = getSession();
		Object obj = session.createQuery(query).uniqueResult();
		return obj;
	}
}
